/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.client.transport.http;

import org.creativor.rayson.share.transport.ByteArrayBuffer;
import org.creativor.rayson.share.transport.http.AbstractHttpMessage;
import org.creativor.rayson.share.transport.http.HttpConstants;
import org.creativor.rayson.share.transport.http.HttpRequest;
import org.creativor.rayson.share.transport.http.HttpRequestLine;

/**
 * Client side HTTP request.
 * 
 * @author creativor
 */
public class ClientHttpRequest extends AbstractHttpMessage implements
		HttpRequest {
	public ClientHttpRequest(final HttpRequestLine requestLine) {
		super(requestLine);
	}

	@Override
	public final HttpRequestLine getStartLine() {
		return (HttpRequestLine) super.getStartLine();
	}

	@Override
	public void setBody(final byte[] body) {
		super.setBody(body);
		final int bodyLength = body.length;
		if (bodyLength == 0) {
			getHeaders().remove(HttpConstants.CONTENT_LENGTH_HEADER_NAME);
		} else {
			getHeaders().set(HttpConstants.CONTENT_LENGTH_HEADER_NAME,
					String.valueOf(bodyLength));
		}

	}

	@Override
	public void writeTo(final ByteArrayBuffer buffer) {
		this.getStartLine().writeTo(buffer);
		getHeaders().writeTo(buffer);
		buffer.write(HttpConstants.CRLF);
		buffer.write(getBody());
	}

}
